package com.zhongying.jf.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/7/7 0007.
 */
public class BaseStringUtils {


    /**
     * 是否是数字
     *
     * @param str 字符串
     * @return boolean true 数字, false 非数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否包含空格
     * @param str
     * @return
     */
    public static boolean isStringHasSpace(String str){
        boolean res = false;
        char[] result = str.toCharArray();
        for(char cha : result){
            if( (int) cha == 32){
                res = true;
                break;
            }
        }
        return res;
    }

    /**
     * 判断字符串是否只包含英文,中文,数字
     */
    public static boolean regexEffectiveLetter(String str){
        String regex = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
        return str.matches(regex);
    }

    /**
     * 判断字符串是否全部为数字
     */
    public static boolean regexEffectiveNumber(String str){
        String regex = "^-?[0-9]+$";
        return str.matches(regex);
    }

    /**
     * 获取手机客户端的版本，型号之类的信息
     * @return
     */
    public static String getClientVersion() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getHeader("user-agent");
    }


    /**
     * 获取客户端来源
     * @return
     */
    public static String getTerminalSouce() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sourceUrl =  request.getHeader("Referer");
        System.out.println(sourceUrl+"____________________");
        return sourceUrl;
    }

    /**
     * Example:
     * @param input
     * @return      input string: He didn't say, "Stop!"
    output string: He didn't say, \"Stop!\"
     * @throws Exception
     */
    public static  String escapeJson(String input) {
        return org.apache.commons.lang3.StringEscapeUtils.escapeJson(input);
    }

    /**
     * 加密敏感字段字符串，用“*”替换敏感字段
     * @param str 原字符串
     * @param frontLength 前面保留多少个字符
     * @param endLength 后面保留多少个字符
     * @return 替换后的字符串
     */
    public static String coverByStar(String str,int frontLength,int endLength){
        if(StringUtils.isNotEmpty(str) && str.length() > frontLength+endLength){
            StringBuilder starBuilder = new StringBuilder();
            String star = "*";
            for (int i = 0; i < str.length() - frontLength - endLength; i++){
                starBuilder.append(star);
            }

            return new StringBuilder().append(str.substring(0,frontLength))
                    .append(starBuilder).append(str.substring(str.length()-endLength)).toString();
        }
        return str;
    }

    /**
     * 脱敏名称
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarName(String str){
        return BaseStringUtils.coverByStar(str,1,0);
    }

    /**
     * 脱敏省份证号，15 18位同样脱敏位置
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarIdcard(String str){
        return BaseStringUtils.coverByStar(str,6,4);
    }

    /**
     * 脱敏手机号
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarMobile(String str){
        return BaseStringUtils.coverByStar(str,3,4);
    }


    /**
     * 脱敏名称
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarName(Object str){
        if(str==null) return null;
        return BaseStringUtils.coverByStar(str.toString(),1,0);
    }

    /**
     * 脱敏省份证号，15 18位同样脱敏位置
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarIdcard(Object str){
        if(str==null) return null;
        return BaseStringUtils.coverByStar(str.toString(),6,4);
    }

    /**
     * 脱敏手机号
     * @param str 原字符串
     * @return 替换后的字符串
     */
    public static String coverByStarMobile(Object str){
        if(str==null) return null;
        return BaseStringUtils.coverByStar(str.toString(),3,4);
    }
}
